package org.wu.framework.doc.platform.controller;

import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;
import org.wu.framework.easy.excel.stereotype.EasyExcel;
import org.wu.framework.easy.excel.util.FastExcelImp;
import org.wu.framework.lazy.orm.core.persistence.conf.LazyTableEndpoint;
import org.wu.framework.lazy.orm.core.source.adapter.SourceFactory;
import org.wu.framework.lazy.orm.database.lambda.stream.lambda.LazyLambdaStream;
import org.wu.framework.web.response.Result;
import org.wu.framework.web.response.ResultFactory;
import org.wu.framework.web.spring.EasyController;

import java.util.HashMap;
import java.util.List;


/**
 * description
 *
 * @author 吴佳伟
 * @date 2023/07/04 23:07
 */
@Tag(name = "DOC-Excel提供者")
@EasyController("/doc/excel")
public class ExcelProvider {

    private final LazyLambdaStream lazyLambdaStream;

    public ExcelProvider(LazyLambdaStream lazyLambdaStream) {
        this.lazyLambdaStream = lazyLambdaStream;
    }

    /**
     * description:json生成Excel
     *
     * @param excelBean excel json 文件
     * @return
     * @author 吴佳伟
     * @date: 4.7.23 23:09
     */
    @EasyExcel(fileName = "json生成Excel")
    @ApiOperation("json生成Excel")
    @PostMapping("/json2Excel")
    public Object json2Excel(@RequestBody Object excelBean) {
        return excelBean;
    }

    /**
     * 将数据导入到数据库指定的表
     *
     * @param file      文件数据
     * @param tableName 表
     */
    @ApiOperation(tags = "DOC-Excel提供者", value = "将数据导入到数据库指定的表")
    @PostMapping("/imp/hash-map/create-table")
    public Result<List<HashMap>> implMapAndCreateTable(
            @RequestPart MultipartFile file,
            @RequestParam String tableName
    ) {
        List<HashMap> hashMapList = FastExcelImp.parseExcel(file, HashMap.class);
        for (HashMap hashMap : hashMapList) {
            LazyTableEndpoint lazyTableEndpoint = SourceFactory
                    .defaultAnalyzeLazyTableFromMap(tableName, hashMap);
            lazyLambdaStream.updateTable(lazyTableEndpoint);
            // 导入数据
            lazyLambdaStream.weakInsert(tableName, hashMap);
        }
        return ResultFactory.successOf(hashMapList);
    }


}
